<template>
    <div class="login-container">
    <div class="login-box">
    <h2 class="title">管理员登录</h2>
    <form @submit.prevent="handleSubmit">
    <div class="form-group">
    <label for="username">用户名</label>
    <input type="text" id="username" v-model="username" placeholder="请输入用户名" required
    class="input-field" />
    </div>
    <div class="form-group">
    <label for="password">密码</label>
    <input type="password" id="password" v-model="password" placeholder="请输入密码" required
    class="input-field" />
    </div>
    <button type="submit" class="submit-btn" :disabled="isLoading">
    <span v-if="isLoading">正在登录...</span>
    <span v-else>登录</span>
    </button>
    </form>
    <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
    </div>
    </div>
    </template>
    
    <script>
    import { ref } from 'vue';
    import { useRouter } from 'vue-router';
    import { login } from '@/api/login'; // 导入 login 函数
    import axios from 'axios';

    import jwt_decode from 'jwt-decode';
    
    export default {
    setup() {
    const router = useRouter();
    const username = ref('');
    const password = ref('');
    const errorMessage = ref('');
    const isLoading = ref(false);
    
    const handleSubmit = async () => {
    if (username.value.trim() === '' || password.value.trim() === '') {
        errorMessage.value = '用户名和密码不能为空';
        return;
    }

    isLoading.value = true;
    errorMessage.value = '';

    try {
        const response = await login(username.value, password.value);
        //console.log(response);  // 打印整个响应

        if (response.data.code === 200) {
            const token = response.data.data;

            // 登录成功，保存 token
            localStorage.setItem('token', token);
            console.log('保存的 token:', token); // 打印 token

            alert('登录成功！');

            // 从 token 中解析 adminID
            const decodedToken = jwt_decode(token);
            console.log(decodedToken);  // 解码后的 token 
            const adminId = decodedToken.sub;
            console.log('adminID:', adminId);

            // 跳转到后台页面
            router.push('/adminpage');
        } else {
            errorMessage.value = '登录失败，请检查用户名和密码';
        }
    } catch (error) {
        console.error(error);
        errorMessage.value = '请求失败，请稍后重试。';
        if (error.response) {
            errorMessage.value = `服务器错误: ${error.response.status}`;
        } else if (error.request) {
            errorMessage.value = '请求超时，请稍后再试';
        }
    } finally {
        isLoading.value = false;
    }
};    
    return {
        username,
        password,
        errorMessage,
        isLoading,
        handleSubmit
    };
    }
    };
    </script>
  

<style scoped>
/* 背景设置渐变效果 */
body {
background: linear-gradient(135deg, #6a11cb, #2575fc);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
color: #fff;
}

/* 登录框样式 */
.login-container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}

.login-box {
background-color: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 40px 50px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
max-width: 420px;
width: 100%;
text-align: center;
transform: scale(1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
transform: scale(1.05);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
}

.title {
font-size: 28px;
font-weight: 700;
color: #333;
margin-bottom: 30px;
}

/* 表单组样式 */
.form-group {
margin-bottom: 20px;
text-align: left;
}

.form-group label {
font-size: 14px;
color: #555;
margin-bottom: 8px;
display: block;
}

.input-field {
width: 100%;
padding: 12px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 8px;
outline: none;
transition: border 0.3s ease, box-shadow 0.3s ease;
}

.input-field:focus {
border-color: #2575fc;
box-shadow: 0 0 5px rgba(37, 117, 252, 0.5);
}

/* 按钮样式 */
.submit-btn {
width: 100%;
padding: 14px;
background-color: #2575fc;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.3s ease;
}

.submit-btn:hover {
background-color: #6a11cb;
transform: translateY(-2px);
}

.submit-btn:disabled {
background-color: #aaa;
cursor: not-allowed;
}

/* 错误提示样式 */
.error-message {
color: #ff4d4f;
margin-top: 20px;
font-size: 14px;
font-weight: 600;
}

/* 响应式设计：适应小屏幕 */
@media (max-width: 480px) {
.login-box {
padding: 20px 30px;
}

.title {
    font-size: 24px;
}

.input-field {
    font-size: 14px;
    padding: 10px;
}

.submit-btn {
    font-size: 14px;
    padding: 12px;
}
}
</style>